package cn.bugstack.test;

import cn.bugstack.infrastructure.persistent.redis.IRedisService;
import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionModel;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionRequest;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;
import io.github.pigmesh.ai.deepseek.core.chat.ResponseFormatType;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.redisson.api.RMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.GetMapping;
import reactor.core.publisher.Flux;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApiTest {

    @Autowired
    private IRedisService redisService;

    @Autowired DeepSeekClient deepSeekClient;

    @Test
    public void test() {
        RMap<Integer, Integer> map = redisService.getMap("strategy_id_100001");
        map.put(1, 101);
        map.put(2, 102);
        map.put(3, 103);
        map.put(4, 104);
        map.put(5, 105);
        log.info("测试结果：{}", redisService.getFromMap("strategy_id_100001", 1).toString());


    }


    @Test
    public void test_chat(){

        String userPrompt = "请你给我介绍一个健身食谱";

        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .temperature(0.9)
                .model(ChatCompletionModel.DEEPSEEK_CHAT)
                .addUserMessage(userPrompt)
                .addSystemMessage("你是一个健身博主")
                .responseFormat(ResponseFormatType.TEXT)
                .build();

        ChatCompletionResponse completionResponse  = deepSeekClient.chatCompletion(request).execute();

        System.out.println(completionResponse.content());

    }




    //迷宫
    public static int[][] grid = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 1, 0},
            {1, 1, 0, 1, 0},
            {0, 0, 0, 1, 0},
            {0, 1, 0, 0, 0}
    };
    public static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    public static int[][] visited = new int[grid.length][grid[0].length];

    public static int result = 0;
    @Test
    public void test_maze(){

        //用visited来记录目前走到的路径的距离
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                visited[i][j] = -1;
            }
        }
        visited[0][0] = 1;
        int bfs = bfs(grid, visited, 0, 0);

        if(bfs == - 1){
            System.out.println(-1);
        }else{
            System.out.println(visited[grid.length][grid[0].length]);
        }

    }


    public static int bfs(int[][] grid, int[][] visited, int x, int y){


        Queue<int[]> q = new ArrayDeque<>();
        q.add(new int[]{x, y});

        while(!q.isEmpty()){
            int[] cur = q.poll();
            int curx = cur[0];
            int cury = cur[1];

            if(curx == grid.length - 1 && cury == grid[0].length - 1){
                return visited[curx][cury];
            }
            for(int i = 0; i < 4; i++){
                int nextX = curx + dirs[curx][i];
                int nextY = cury + dirs[cury][i];
                if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length){
                    continue;
                }
                if(grid[nextX][nextY] == 0 && visited[nextX][nextY] == -1){
                    q.add(new int[]{nextX, nextY});
                    visited[nextX][nextY] = visited[x][y] + 1;
                }
            }
        }
        return -1;
    }




    public static void main(String[] args) {
        int[][] maze = {
                {0, 1, 0, 0, 0},
                {0, 0, 0, 1, 0},
                {1, 1, 0, 1, 0},
                {0, 0, 0, 1, 0},
                {0, 1, 0, 0, 0}
        };
        int result = shortestPath(maze);
        System.out.println("Shortest Path Length: " + result);
    }

    public static int shortestPath(int[][] maze) {
        if (maze == null || maze.length == 0 || maze[0].length == 0) {
            return -1; // Invalid maze input
        }

        int rows = maze.length;
        int cols = maze[0].length;

        // Check if entrance or exit is blocked
        if (maze[0][0] != 0 || maze[rows - 1][cols - 1] != 0) {
            return -1;
        }

        // Directions: up, down, left, right
        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

        // Initialize visited matrix to track the shortest path length
        int[][] visited = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                visited[i][j] = -1;
            }
        }
        visited[0][0] = 1; // Path length starts at 1 for the entrance

        // BFS queue
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{0, 0});

        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int x = current[0];
            int y = current[1];

            // Check if current position is the exit
            if (x == rows - 1 && y == cols - 1) {
                return visited[x][y];
            }

            // Explore all four directions
            for (int[] dir : dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];

                // Check boundaries and if the cell is accessible and unvisited
                if (nx >= 0 && nx < rows && ny >= 0 && ny < cols) {
                    if (maze[nx][ny] == 0 && visited[nx][ny] == -1) {
                        visited[nx][ny] = visited[x][y] + 1;
                        queue.add(new int[]{nx, ny});
                    }
                }
            }
        }

        // If no path found
        return visited[rows - 1][cols - 1];
    }



}
